package cn.kiliter.web.servlet;

import cn.kiliter.model.PageResult;
import cn.kiliter.model.Product;
import cn.kiliter.serviece.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/showProductByPage")
public class ShowPageListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String category = request.getParameter("category");
        String currentPagestr = request.getParameter("currentPage");
        int pageSize=4;
        int currentPage=1;
        if(currentPagestr!=null&&!"".equals(currentPagestr)){
             currentPage = Integer.parseInt(currentPagestr);
        }
        ProductService service=new ProductService();
        PageResult<Product> pageBooks = service.findPageBooks(currentPage, pageSize, category);
        request.setAttribute("pr",pageBooks);
        request.setAttribute("category",category);
        request.getRequestDispatcher("/product_list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
